# coding: utf8

#########################################################################
## This scaffolding model makes your app work on Google App Engine too
#########################################################################

if request.env.web2py_runtime_gae:            # if running on Google App Engine
    db = DAL('gae')                           # connect to Google BigTable
#    session.connect(request, response, db=db) # and store sessions and tickets there
    ### or use the following lines to store sessions in Memcache
    from gluon.contrib.memdb import MEMDB
    from google.appengine.api.memcache import Client
    session.connect(request, response, db=MEMDB(Client()))
else:                                         # else use a normal relational database
    db = DAL('sqlite://storage.sqlite')       # if not, use SQLite or other DB
## if no need for session
# session.forget()

#########################################################################
## Here is sample code if you need for 
## - email capabilities
## - authentication (registration, login, logout, ... )
## - authorization (role based authorization)
## - services (xml, csv, json, xmlrpc, jsonrpc, amf, rss)
## - crud actions
## comment/uncomment as needed

from gluon.tools import *
auth=Auth(globals(),db)                      # authentication/authorization
auth.define_tables()                         # creates all needed tables
crud=Crud(globals(),db)                      # for CRUD helpers using auth
service=Service(globals())                   # for json, xml, jsonrpc, xmlrpc, amfrpc

# crud.settings.auth=auth                      # enforces authorization on crud
# mail=Mail()                                  # mailer
# mail.settings.server='smtp.gmail.com:587'    # your SMTP server
# mail.settings.sender='you@gmail.com'         # your email
# mail.settings.login='username:password'      # your credentials or None
# auth.settings.mailer=mail                    # for user email verification
# auth.settings.registration_requires_verification = True
# auth.settings.registration_requires_approval = True
# auth.messages.verify_email = \
#  'Click on the link http://.../user/verify_email/%(key)s to verify your email'
## more options discussed in gluon/tools.py
#########################################################################

#########################################################################
## Define your tables below, for example
##
## >>> db.define_table('mytable',Field('myfield','string'))
##
## Fields can be 'string','text','password','integer','double','boolean'
##       'date','time','datetime','blob','upload', 'reference TABLENAME'
## There is an implicit 'id integer autoincrement' field
## Consult manual for more options, validators, etc.
##
## More API examples for controllers:
##
## >>> db.mytable.insert(myfield='value')
## >>> rows=db(db.table.myfield=='value).select(db.mytable.ALL)
## >>> for row in rows: print row.id, row.myfield
#########################################################################


########################################################################
## Application Show database start
########################################################################
import datetime; now=datetime.datetime.now()

db.define_table('tags',
    SQLField('tag'))
db.tags.tag.requires = [IS_NOT_EMPTY(error_message='内容是空的!'), IS_NOT_IN_DB(db, 'tags.tag')]


db.define_table('images',
    SQLField('title'),
    SQLField('file', 'upload'))
db.images.title.requires = [
        IS_NOT_EMPTY(error_message='内容是空的!'),
        IS_NOT_IN_DB(db, 'images.title', error_message='内容重复!')]
db.images.file.requires = IS_IMAGE()


db.define_table('categories',
    SQLField('category'))
db.categories.category.requires = [
        IS_NOT_EMPTY(error_message='内容是空的!'),
        IS_NOT_IN_DB(db, 'categories.category', error_message='内容重复!')]
    

db.define_table('people',
    SQLField('name'),
    SQLField('email'),
    SQLField('phone'))


db.define_table('setting',
    SQLField('peole', db.people),
    SQLField('page_number'))


db.define_table('templates',
    SQLField('title'),
    SQLField('category', db.categories),
    SQLField('tag', db.tags),
    SQLField('swfpath', default='swfs'),
    SQLField('swfname'),
    SQLField('image', db.images),
    SQLField('posted_by'),
    SQLField('posted_on', 'datetime', default=now),
    SQLField('description', 'text'),
    SQLField('code', 'text'))
db.templates.title.requires = IS_NOT_EMPTY()
db.templates.category.requires = IS_IN_DB(db, 'categories.id', '%(category)s')
db.templates.tag.requires = IS_IN_DB(db, 'tags.id', '%(tag)s')
db.templates.swfpath.requires = IS_NOT_EMPTY()
db.templates.swfname.requires = IS_NOT_EMPTY()
db.templates.image.requires = IS_IN_DB(db, 'images.id', '%(title)s')
db.templates.posted_by.requires = [IS_NOT_EMPTY()] # default=session.auth.user.first_name+session.auth.user.last_name

    
db.define_table('favorite',
    SQLField('template', db.templates),
    SQLField('people', db.people))

